//
//  BMMyViewController.m
//  EnjogFood
//
//  Created by 王芳 on 17/4/30.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "BMMyViewController.h"
#import "EFMySubviewItem.h"
#import "EFCouponViewController.h"
#import "EFMyDeliveryAddressViewController.h"
#import "EFMyCollectionViewController.h"
#import "EFOrderLogisticsInformationViewController.h"
#import "EFMyOrderViewController.h"
#import "EFLoginViewController.h"
#import "EFReturnListViewController.h"
#import "EFDistributorViewController.h"
#import "EFMyDistributorViewController.h"
#import "EFOrderDetailViewController.h"
#import "EFTransHomeDetailViewController.h" 
#import "EFRequestReturnViewController.h"
#import "EFGoodsCategoryViewController.h"
#import "EFLogoutViewItem.h"
#import "EFLogoutViewCell.h"
#import "EFUsePointsViewController.h"
#import "iToast.h"

@interface BMMyViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)UIView* topBgView;
@property(strong,nonatomic)UIView* topContentView;
@property(strong,nonatomic)UIImageView* qCodeView;
@property(strong,nonatomic)UIImageView* headerIV;
@property(strong,nonatomic)UILabel* nameLabel;

@property(strong,nonatomic)UIView* midBgView;
@property(strong,nonatomic)UILabel* scoreLabel;
@property(strong,nonatomic)UILabel* scoreTipsLabel;
@property(strong,nonatomic)UIView* midLineView;

@property(strong,nonatomic)UIView* couponView;
@property(strong,nonatomic)UILabel* couponLabel;
@property(strong,nonatomic)UILabel* couponTipsLabel;


@property(strong,nonatomic)UITableView* tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, assign) NSInteger reseller;

@property(assign,nonatomic)Boolean bDistributor;

@end

@implementation BMMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeDistributor) name:IC_REQUEST_DISTRIBUTOR_NOTIFICATION object:nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [self initData];
}

-(instancetype)init{
    self = [super init];
    if (self) {
        self.dataArray = [NSMutableArray array];
    }
    return self;
}

-(void)changeDistributor{
    [self initData];
}

-(void)initView{
    
    [self.view addSubview:self.topBgView];
    [self.topBgView addSubview:self.topContentView];
    [self.topContentView addSubview:self.qCodeView];
//    [self.topBgView addSubview:self.headerIV];
//    [self.topBgView addSubview:self.nameLabel];
    
    [self.view addSubview:self.midBgView];
    [self.midBgView addSubview:self.scoreLabel];
    [self.midBgView addSubview:self.scoreTipsLabel];
    [self.midBgView addSubview:self.midLineView];
    [self.couponView addSubview:self.couponLabel];
    [self.couponView addSubview:self.couponTipsLabel];
    [self.midBgView addSubview:self.couponView];
    
//    
//    EFMySubviewItem* item1 = [[EFMySubviewItem alloc]init];
//    item1.name = @"我的订单";
//    [self.dataArray addObject:item1];
//    
////    EFMySubviewItem* item2 = [[EFMySubviewItem alloc]init];
////    item2.name = @"我的社区";
////    [self.dataArray addObject:item2];
////    
////    EFMySubviewItem* item3 = [[EFMySubviewItem alloc]init];
////    item3.name = @"我的评论";
////    [self.dataArray addObject:item3];
//    
//    EFMySubviewItem* item4 = [[EFMySubviewItem alloc]init];
//    item4.name = @"我的收藏";
//    [self.dataArray addObject:item4];
//    
//    EFMySubviewItem* item5 = [[EFMySubviewItem alloc]init];
//    item5.name = @"收货地址";
//    [self.dataArray addObject:item5];
//    
//    EFMySubviewItem* item6 = [[EFMySubviewItem alloc]init];
//    item6.name = @"成为分销商";
//    [self.dataArray addObject:item6];
//    
////    EFMySubviewItem* item3 = [[EFMySubviewItem alloc]init];
////    item3.name = @"申请退货";
////    [self.dataArray addObject:item3];
//
//    
//    EFLogoutViewItem* item7 = [[EFLogoutViewItem alloc]init];
//    [self.dataArray addObject:item7];
//    
//    
    [self.view addSubview:self.tableView];
    
    self.bDistributor = false;
}

-(void)initItem{
    [self.dataArray removeAllObjects];
    
    EFMySubviewItem* item1 = [[EFMySubviewItem alloc]init];
    item1.name = @"我的订单";
    [self.dataArray addObject:item1];
    
    //    EFMySubviewItem* item2 = [[EFMySubviewItem alloc]init];
    //    item2.name = @"我的社区";
    //    [self.dataArray addObject:item2];
    //
    //    EFMySubviewItem* item3 = [[EFMySubviewItem alloc]init];
    //    item3.name = @"我的评论";
    //    [self.dataArray addObject:item3];
    
    EFMySubviewItem* item4 = [[EFMySubviewItem alloc]init];
    item4.name = @"我的收藏";
    [self.dataArray addObject:item4];
    
    EFMySubviewItem* item5 = [[EFMySubviewItem alloc]init];
    item5.name = @"收货地址";
    [self.dataArray addObject:item5];
    
    if(self.reseller != 0){
        EFMySubviewItem* item6 = [[EFMySubviewItem alloc]init];
        if(self.reseller==1){
            item6.name = @"成为分销商";
        }else if(self.reseller ==2){
            item6.name = @"我的分销";
        }
        [self.dataArray addObject:item6];
    }
    
    //    EFMySubviewItem* item3 = [[EFMySubviewItem alloc]init];
    //    item3.name = @"申请退货";
    //    [self.dataArray addObject:item3];
    
    EFLogoutViewItem* item7 = [[EFLogoutViewItem alloc]init];
    [self.dataArray addObject:item7];
    
    [self.tableView reloadData];
}

-(void)initData{
    [APP_CONTEXT.net myAccount:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            if ([info objectForKey:@"avatar"]) {
                NSString *headerImg = [info objectForKey:@"avatar"];
                if(headerImg != nil){
                    [self.headerIV setImageWithURL:[NSURL URLWithString:headerImg]];
                }
            }
            if ([info objectForKey:@"points"]) {
                NSString *points = [info objectForKey:@"points"];
                [self.scoreLabel setText:points];
            }else{
                 [self.scoreLabel setText:@"0"];
            }
            [self.scoreLabel sizeToFit];
            _scoreLabel.left = (kScreenWidth/2 - _scoreLabel.width)/2;
            
            if ([info objectForKey:@"couponQty"]) {
                NSString *coupon = [info objectForKey:@"couponQty"];
                [self.couponLabel setText:coupon];
            }else{
                 [self.couponLabel setText:@"0"];
            }
            [_couponLabel sizeToFit];
            _couponLabel.left =   (kScreenWidth/2 -  _couponLabel.width)/2;
            
            if([info objectForKey:@"membershipCardBarcode"]){
                NSString *qCode = [info objectForKey:@"membershipCardBarcode"];
                [self.qCodeView setImageWithURL:[NSURL URLWithString:qCode]];
            }
            if([info objectForKey:@"reseller"]){
                self.reseller = [[info objectForKey:@"reseller"] integerValue];
            }
            
            [self initItem];
        }
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem* item = self.dataArray[indexPath.row];
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    
    if ([item isKindOfClass:[EFLogoutViewItem class]]) {
        ((EFLogoutViewCell *)cell).logoutblock = ^{
            APP_CONTEXT.addressManager.defaultAddress = nil;
            [APP_CONTEXT.addressManager persistenceAddressManager];
            [[ICNavigator sharedInstance].tabBarController.tabBar selectedItemAtIndex:0];
        };
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger index = indexPath.row;
    switch (index) {
        case 0:{
            EFMyOrderViewController *order = [[EFMyOrderViewController alloc] init];
            [ICNavigator pushViewController:order animated:YES];
            
            break;
        }
//        case 1:{
//            EFReturnListViewController* returnList = [[EFReturnListViewController alloc] init];
//            [ICNavigator pushViewController:returnList animated:YES];
//            EFGoodsCategoryViewController* goodCategory = [[EFGoodsCategoryViewController alloc]initWithCategoryId:@"111"];
//            [ICNavigator pushViewController:goodCategory animated:YES];
//
//            
////            EFTransHomeDetailViewController *detail = [[EFTransHomeDetailViewController alloc] init];
////            [ICNavigator pushViewController:detail animated:YES];
//
//            break;
//        }
//        case 2:{
//            EFRequestReturnViewController* request = [[EFRequestReturnViewController alloc]init];
//             [ICNavigator pushViewController:request animated:YES];
//            break;
//        }
        case 1:{
            EFMyCollectionViewController *collection = [[EFMyCollectionViewController alloc] init];
            [ICNavigator pushViewController:collection animated:YES];
            break;
        }
        case 2:{
            EFMyDeliveryAddressViewController *address = [[EFMyDeliveryAddressViewController alloc] init];
            [ICNavigator pushViewController:address animated:YES];
            break;
        }
        case 3:{
           if(self.reseller == 2){
                EFMyDistributorViewController *distributor = [[EFMyDistributorViewController alloc] init];
                [ICNavigator pushViewController:distributor animated:YES];
            }else if(self.reseller == 1){
                [APP_CONTEXT.net checkReseller:^(NSInteger code, NSDictionary *info, NSString *message) {
                    if(code == 0){
                        if ([info objectForKey:@"data"]) {
                            NSNumber* data = [info objectForKey:@"data"];
                            NSString* msg = [info objectForKey:@"msg"];
                            if(data.integerValue == 1){
                                [[iToast makeText:msg]show];
                            }else{
                                EFDistributorViewController *distributor = [[EFDistributorViewController alloc] init];
                                [ICNavigator pushViewController:distributor animated:YES];
                            }
                        }
                    }
                }];
            }
            break;
        }
//        case 4:{
//            EFRequestReturnViewController* request = [[EFRequestReturnViewController alloc]initWithOrderNo:@"376111893"];
//            [ICNavigator pushViewController:request animated:YES];
////            EFReturnListViewController* returnList = [[EFReturnListViewController alloc] initWithOrderId:@"11111"];
////            [ICNavigator pushViewController:returnList animated:YES];
//
//            break;
//        }
        default:
            break;
    }
    
}


-(UIView*)topBgView{
    if(_topBgView == NULL){
        _topBgView = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, kScreenWidth, 196 )];
        _topBgView.backgroundColor = RGBCOLOR(0x7EAD6A);
    }
    return _topBgView;
}

-(UIView *)topContentView{
    if(_topContentView == NULL){
        _topContentView = [[UIView alloc ]initWithFrame:CGRectMake(20, 20, kScreenWidth - 40, 186)];
        _topContentView.backgroundColor = [UIColor whiteColor];
        _topContentView.layer.cornerRadius = 10;
        _topContentView.layer.masksToBounds = true;
    }
    return _topContentView;
}

-(UIImageView *)qCodeView{
    if(_qCodeView == NULL){
        _qCodeView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 325 , 166)];
        _qCodeView.backgroundColor = RGBCOLOR(0xF3F3F3);
        _qCodeView.left = (self.topContentView.width - self.qCodeView.width)/2;
        _qCodeView.top = 10;
    }
    return _qCodeView;
}

-(UIImageView*)headerIV{
    if(_headerIV == NULL){
        _headerIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 78, 78)];
        _headerIV.layer.cornerRadius = 39;
        _headerIV.layer.borderWidth = 2;
        _headerIV.layer.borderColor = [UIColor whiteColor].CGColor;
        _headerIV.left = (kScreenWidth - 78)/2;
        _headerIV.top = 10;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickHeader)];
        [_headerIV addGestureRecognizer:tap];
        _headerIV.userInteractionEnabled = YES;
        _headerIV.contentMode = UIViewContentModeScaleAspectFill;
        _headerIV.clipsToBounds = YES;
    }
    return _headerIV;
}

-(UILabel*)nameLabel{
    if(_nameLabel == NULL){
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 15)];
        _nameLabel.textColor =  [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:17];
        

        [_nameLabel setText:@"Lily Yuan"];
        [_nameLabel sizeToFit ];
        _nameLabel.left =(kScreenWidth - self.nameLabel.width)/2;
        _nameLabel.top = self.headerIV.bottom + 6;
        
    }
    return _nameLabel;
}

-(UIView*)midBgView{
    if(_midBgView == NULL){
        _midBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 76)];
        _midBgView.backgroundColor = RGBCOLOR(0xF5F9F9);
        _midBgView.top = self.topBgView.bottom;
    }
    return _midBgView;
}

-(UILabel*)scoreLabel{
    if(_scoreLabel == NULL){
        _scoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 65, 21)];
        _scoreLabel.font = [UIFont systemFontOfSize:23];
        _scoreLabel.textColor = kBlack333;
        _scoreLabel.left = (kScreenWidth/2 - 65)/2;
        _scoreLabel.top =  19;
        [_scoreLabel setText:@"0"];
        [self.scoreLabel sizeToFit];
        _scoreLabel.left = (kScreenWidth/2 - _scoreLabel.width)/2;
    }
    return _scoreLabel;
}

-(UILabel*)scoreTipsLabel{
    if(_scoreTipsLabel == NULL){
        _scoreTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 21)];
        _scoreTipsLabel.font = [UIFont systemFontOfSize:13];
        _scoreTipsLabel.textColor = kBlack999;
        _scoreTipsLabel.left = (kScreenWidth/2 - 65)/2;
        _scoreTipsLabel.top =  self.scoreLabel.bottom + 2;
        [_scoreTipsLabel setText:@"我的积分"];
        [_scoreTipsLabel sizeToFit];
        _scoreTipsLabel.left = (kScreenWidth/2 - _scoreTipsLabel.width)/2;
    }
    return _scoreTipsLabel;
}

-(UIView*)midLineView{
    if(_midLineView == NULL){
        _midLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 35)];
        _midLineView.backgroundColor = RGBCOLOR(0xECECEC);
        _midLineView.left = (kScreenWidth - 1)/2;
        _midLineView.top = 20;
    }
    return _midLineView;
}


-(UIView*)couponView{
    if(_couponView == NULL){
        _couponView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth/2 - 10, 76)];
        _couponView.backgroundColor =  RGBCOLOR(0xF5F9F9);
        _couponView.top = 0;
        _couponView.left = self.midLineView.right ;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCoupon)];
        [_couponView addGestureRecognizer:tap];
        _couponView.userInteractionEnabled = YES;
    }
    return _couponView;
}

-(UILabel*)couponLabel{
    if(_couponLabel == NULL){
        _couponLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 65, 21)];
        _couponLabel.font = [UIFont systemFontOfSize:23];
        _couponLabel.textColor = kBlack333;
        _couponLabel.top =  19;
        [_couponLabel setText:@"0"];
        [_couponLabel sizeToFit];
        _couponLabel.left = (kScreenWidth/2 - _scoreLabel.width)/2;
        
        
    }
    return _couponLabel;
}

-(UILabel*)couponTipsLabel{
    if(_couponTipsLabel == NULL){
        _couponTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 65, 21)];
        _couponTipsLabel.font = [UIFont systemFontOfSize:13];
        _couponTipsLabel.textColor = kBlack999;
        _couponTipsLabel.top =  self.couponLabel.bottom + 2 ;
        [_couponTipsLabel setText:@"优惠券"];
        [_couponTipsLabel sizeToFit];
        _couponTipsLabel.left =   (kScreenWidth/2 -  _couponTipsLabel.width)/2;
        
    }
    return _couponTipsLabel;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 218 - 76 - 50)];
        _tableView.top = self.midBgView.bottom;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

-(void)onClickHeader{
    
//    EFLoginViewController *login = [[EFLoginViewController alloc] init];
//    [ICNavigator pushViewController:login animated:YES];
}

-(void)onClickCoupon{
    EFCouponViewController *coupon = [[EFCouponViewController alloc] init];
    [ICNavigator pushViewController:coupon animated:YES];
}

@end
